const {
    depthUpgrade,   //深度升级
    qualityUpgrade,   //品质升级
    spreedUpgrade,   //速度升级
    getLogOutCoinsByMultiple,   //根据倍数领取离线金币数量
    getOpenInfoAction,
    getUserFloorAction,
    openFloorAction,
    upgradeFloorAction
} = require('../lib/action');
const {
    Msg,
    Code
} = require('../lib/consts');

module.exports = function (app) {
    return new Handler(app);
};
let Handler = function (app) {
    this.app = app;
};
let handler = Handler.prototype;

//深度升级
handler.depthUpgrade = async function (msg, session, next) {
    let {uid, frontendId} = session;
    try {
        let {depthGrade, depthNum, incomeRate, depthCoinsume} = await depthUpgrade.call(this, uid, frontendId);
        //depthGrade 当前深度等级  depthNum 当前深度  incomeRate 采集收入速度  depthCoinsume 当前深度消耗
        next(null, {code: 200, depthGrade, depthNum, incomeRate, depthCoinsume});
    } catch (err) {
        next(err, {code: 500, msg: err.message});
    }
}

//品质升级
handler.qualityUpgrade = async function (msg, session, next) {
    let {uid, frontendId} = session;
    try {
        let {qualityGrade, incomeRate, qualityConsume} = await qualityUpgrade.call(this, uid, frontendId);
        //当前品质等级和采集收入速度，品质消耗
        next(null, {code: 200, qualityGrade, incomeRate, qualityConsume});
    } catch (err) {
        next(err, {code: 500, msg: err.message});
    }
}

//速度升级
handler.speedUpgrade = async function (msg, session, next) {
    let {uid, frontendId} = session;
    try {
        let {spreedGrade, incomeRate, spreedCoinsConsume} = await spreedUpgrade.call(this, uid, frontendId);
        //当前速度等级和采集收入速度，速度消耗
        next(null, {code: 200, spreedGrade, incomeRate, spreedCoinsConsume});
    } catch (err) {
        next(err, {code: 500, msg: err.message});
    }
}

//普通领取离线金币
handler.getLogoutCoins = async function (msg, session, next) {
    let {uid} = session;
    try {
        await getLogOutCoinsByMultiple.call(this, session,uid);
        next(null, {code: 200});
    } catch (err) {
        next(err, {code: 500, msg: err.message});
    }
}

//双倍领取离线金币
handler.getDoubleLogoutCoins = async function (msg, session, next) {
    let {uid} = session;
    try {
        await getLogOutCoinsByMultiple.call(this, session,uid, 2);
        next(null, {code: 200});
    } catch (err) {
        next(err, {code: 500, msg: err.message});
    }
}

/**
 * 获取起始矿层后固定数量的矿层的解锁花费
 * @param msg
 * @param session
 * @param next
 */
handler.c2sGetOpenCoins = async function (msg, session, next) {
    let self = this;
    let floor = parseInt(msg.floor);
    let count = parseInt(msg.count);
    try {
        let ret = await getOpenInfoAction.call(self, floor, count);
        return next(null, {
            code: Code.SUC,
            msg: Msg.RESULT_SUCCESS,
            data: ret
        });
    } catch (err) {
        return next(null, {
            code: Code.ERR,
            msg: err.message
        });
    }
}

/**
 * 获取当前用户的矿层信息
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sGetLevelInfo = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');

    try {
        let ret = await getUserFloorAction.call(self, uid);
        return next(null, {
            code: Code.SUC,
            msg: Msg.RESULT_SUCCESS,
            data: ret
        });
    } catch (err) {
        console.error('c2sGetLevelInfo err: ' + err);
        return next(null, {
            code: Code.ERR,
            msg: err.message
        });
    }
}

/**
 * 解锁矿层
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sOpenFloor = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    let floor = msg.floor;

    try {
        let ret = await openFloorAction.call(self, session, uid, floor);
        return next(null, {
            code: Code.SUC,
            msg: Msg.RESULT_SUCCESS,
            data: ret
        });
    } catch (err) {
        console.error('c2sOpenFloor err: ' + err);
        return next(null, {
            code: Code.ERR,
            msg: err.message
        });
    }
}

/**
 * 升级矿层
 * @param msg
 * @param session
 * @param next
 */
handler.c2sUpgradeFloor = async function (msg, session, next) {
    let self = this;
    let floor = msg.floor;
    let uid = session.get('uid');

    try {
        let ret = await upgradeFloorAction.call(self, session, uid, floor);
        return next(null, {
            code: Code.SUC,
            msg: Msg.RESULT_SUCCESS,
            data: ret
        });
    } catch (err) {
        console.error('c2sUpgradeFloor err: ' + err);
        return next(null, {
            code: Code.ERR,
            msg: err.message
        });
    }
}